<?php
class ActionsController extends AppController{
    var $name='Actions';
function  beforeFilter() {
            parent::beforeFilter();
            $this->Auth->autoRedirect = false;
            $this->set('active',3);
        }
    /*
     * @param integer $type type of action
     * @param integer $dmdId dmdform id
     * @return void
     * @access public
     */
    function _createAction($type,$dmdId){
        $data['Action']['action_type']=$type;
        $data['Action']['action_by']=$this->Auth->user('id');
        $data['Action']['dmdform_id']=$dmdId;
        $data['Action']['notes']=  $this->Action->Dmdform->getCode($dmdId);
        $this->Action->save($data);
    }
    
    function index() {
		$this->Action->recursive = 0;
		$this->set('actions', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid action', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('action', $this->Action->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Action->create();
			if ($this->Action->save($this->data)) {
				$this->Session->setFlash(__('The action has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The action could not be saved. Please, try again.', true));
			}
		}
		$dmdforms = $this->Action->Dmdform->find('list');
		$dmdformstatuses = $this->Action->Dmdformstatus->find('list');
		$this->set(compact('dmdforms', 'dmdformstatuses'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid action', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Action->save($this->data)) {
				$this->Session->setFlash(__('The action has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The action could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Action->read(null, $id);
		}
		$dmdforms = $this->Action->Dmdform->find('list');
		$dmdformstatuses = $this->Action->Dmdformstatus->find('list');
		$this->set(compact('dmdforms', 'dmdformstatuses'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for action', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Action->delete($id)) {
			$this->Session->setFlash(__('Action deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Action was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

}

?>
